// pagesMine/pages/recruitmentManage/recruitmentManage.js
import ajax from '../../../utils/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /**
     * tabs按钮状态
     */
    tabActive: 0,
    /**
     * 下拉刷新状态
     */
    refresher: false,
    /**
     * 应聘简历数据
     */
    jobsMyList: [],
    /**
     * 岗位管理数组
     */
    postList: [],
    /**
     * 应聘简历对象
     */
    formDataJobs: {
      pageSize: 10,
      pageNum: 1,
      total: 0
    },
    /**
     * 岗位管理对象
     */
    formDataPost: {
      pageSize: 10,
      pageNum: 1,
      total: 0
    },
    modeShow: false,
    modeItem: null
  },
  /**
   * 招聘信息管理 操作
   */
  async modeEvent(event) {
    console.log(event, this.data.modeItem)
    const type = event.currentTarget.dataset.type
    const entity = this.data.modeItem
    if (type == 3) {
      this.setModeShow()
      return wx.navigateTo({ url: `/pagesEnterprise/pages/publishJob/publishJob?detailId=${entity.id}` })
    }
    ajax.showLoading()
    await ajax.post('/enterpriseservice/recruit/jobs/updateJobs', {
      id: entity.id,
      orderStatus: type, // 订单状态：0招聘中；1招聘成功；2停止招聘
    }).finally(() => wx.hideLoading())
    ajax.showToast('操作成功')
    this.setModeShow()
    this.data.formDataPost.pageNum = 1
    this.getPostList()
  },
  /**
   * 取消操作
   */
  setModeShow() {
    this.setData({ modeShow: false })
  },
  /**
   * 岗位点击事件
   */
  recruitmentClick(event) {
    const { tabActive } = this.data
    if (tabActive == 1) return this.setData({ modeShow: true, modeItem: event.detail })
    wx.navigateTo({ url: `/pagesMine/pages/resume/resume?detailId=${event.detail.id}` })
  },
  /**
   * 查询应聘简历列表
   */
  async getJobsMyList() {
    const { formDataJobs, jobsMyList } = this.data
    const res = await ajax.get('/enterpriseservice/recruit/my/jobsMyList', formDataJobs).finally(() => this.setRefresher(false))
    const list = res.rows
    this.data.formDataJobs.total = res.total - 0
    this.setData({ jobsMyList: formDataJobs.pageNum == 1 ? list : jobsMyList.concat(list) })
  },
  /**
   * 查询招聘列表
   */
  async getPostList() {
    const { formDataPost, postList } = this.data
    const res = await ajax.get('/enterpriseservice/recruit/jobs/postList', formDataPost).finally(() => this.setRefresher(false))
    const list = res.rows
    this.data.formDataPost.total = res.total - 0
    this.setData({ postList: formDataPost.pageNum == 1 ? list : postList.concat(list) })
  },
  /**
   * tab切换事件
   */
  tabEvent(event) {
    const type = event.currentTarget.dataset.type
    const tabActive = type == 'swiper' ? event.detail.current : event.currentTarget.dataset.value
    if (this.data.tabActive == tabActive) return
    this.setData({ tabActive })
  },
  setRefresher(refresher) {
    this.setData({ refresher })
  },
  buttonEvent() {
    wx.navigateTo({
      url: '/pagesEnterprise/pages/publishJob/publishJob',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getJobsMyList()
    this.getPostList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  PullDownRefresh() {
    this.data.tabActive == 0 ? this.data.formDataJobs.pageNum = 1 : this.data.formDataPost.pageNum = 1
    this.setRefresher(true)
    this.data.tabActive == 0 ? this.getJobsMyList() : this.getPostList()
  },
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  ReachBottom() {
    const { jobsMyList, postList, tabActive, formDataJobs, formDataPost } = this.data
    const total = tabActive == 0 ? formDataJobs.total : formDataPost.total
    const list = tabActive == 0 ? jobsMyList : postList
    if (list.length >= total) return
    tabActive == 0 ? this.data.formDataJobs.pageNum += 1 : this.data.formDataPost.pageNum += 1
    tabActive == 0 ? this.getJobsMyList() : this.getPostList()
  },
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})